package com.lzx.shop.api;

import com.lzx.shop.model.Result;
import com.lzx.shop.service.OssService;
import com.lzx.shop.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName OssApi.java
 * @Description TODO
 * @createTime 2021年04月10日 21:01:00
 */
@RestController
@RequestMapping("/oss")
public class OssApi {

    @Autowired
    private OssService ossService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam(value = "file",required = false) MultipartFile multipartFile){

        String url = ossService.upload(multipartFile);

        return ResultUtil.customResultParam(true,"0","success",url);
    }

    @RequestMapping("/batchUpload")
    public Result batchUpload(@RequestParam(value = "file",required = false) MultipartFile[] multipartFile){

        StringBuilder stringBuilder = new StringBuilder();
        for (MultipartFile file : multipartFile) {

        }
        for (int i = 0; i < multipartFile.length; i++) {
            stringBuilder.append(ossService.upload(multipartFile[i]));
            if (i!=multipartFile.length-1){
                stringBuilder.append(" ");
            }
        }
        return ResultUtil.customResultParam(true,"0","success",stringBuilder.toString());
    }

    @GetMapping("/del")
    public Result delFile(@RequestParam("name")String name){

        return ResultUtil.success(ossService.delete(name)) ;

    }

}
